package com.chinawayltd.ews.sample.service.basicInfo.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinawayltd.ews.sample.mapper.TDictInfoMapper;
import com.chinawayltd.ews.sample.mapper.TDictTypeMapper;
import com.chinawayltd.ews.sample.mapper.TLogMapper;
import com.chinawayltd.ews.sample.mapper.basicInfo.DictExtendMapper;
import com.chinawayltd.ews.sample.model.TDictInfo;
import com.chinawayltd.ews.sample.model.TDictType;
import com.chinawayltd.ews.sample.model.TLog;
import com.chinawayltd.ews.sample.model.basicInfo.DictInfoVO;
import com.chinawayltd.ews.sample.service.basicInfo.DictService;
import com.chinawayltd.ews.sample.web.query.DictTypeGirdQuery;

/**
 * @program: greeProject
 * @description 字典类型及字典项实现类
 * @author: chenxin
 * @create: 2018年5月11日 下午10:43:27
 */
@Service
public class DictServiceImpl implements DictService{
    @Autowired
    private TLogMapper tLogMapper;
    private final String tableDictTypeName = "T_DICT_TYPE";
    private final String tableDictInfoName = "T_DICT_INFO";
    @Autowired
    private DictExtendMapper dictExtendMapper;
    
    @Autowired
    private TDictTypeMapper tDictTypeMapper;
    
    @Autowired
    private TDictInfoMapper tDictInfoMapper;

	@Override
	public List<TDictType> listDictType(DictTypeGirdQuery queryParam) throws Exception {
		return dictExtendMapper.listDictType(queryParam);
	}

	@Override
	public TDictType getDictTypeById(Long id) throws Exception {
		if(id == null || id <= 0){
            throw new RuntimeException("id不能为空或者少于等于0！");
        }
		return tDictTypeMapper.selectByPrimaryKey(id);
	}

	@Override
	@Transactional
	public int addDictType(TDictType tDictType) throws Exception {
		Date date = new Date();
		tDictType.setCreateTime(date);
        int result = dictExtendMapper.insertDictType(tDictType);
        if(result >0){
    		TLog log = new TLog();
    		log.setDataId(tDictType.getId());
    		log.setTableName(tableDictTypeName);
    		log.setOperationType(1);
    		log.setCreateTime(date);
    		tLogMapper.insert(log);
        }
        return result;
	}

	@Override
	@Transactional
	public int addDictInfo(TDictInfo tDictInfo) throws Exception {
		Date date = new Date();
		tDictInfo.setCreateTime(date);
        int result = dictExtendMapper.insertDictInfo(tDictInfo);
        if(result >0){
    		TLog log = new TLog();
    		log.setDataId(tDictInfo.getId());
    		log.setTableName(tableDictInfoName);
    		log.setOperationType(1);
    		log.setCreateTime(date);
    		tLogMapper.insert(log);
        }
        return result;
	}

	@Override
	@Transactional
	public int updateDictType(TDictType tDictType) throws Exception {
		Date date = new Date();
		tDictType.setUpdateTime(date);
        int result = tDictTypeMapper.updateByPrimaryKey(tDictType);
        if(result >0){
    		TLog log = new TLog();
    		log.setDataId(tDictType.getId());
    		log.setTableName(tableDictTypeName);
    		log.setOperationType(2);
    		log.setCreateTime(date);
    		tLogMapper.insert(log);
        }
        return result;
	}

	@Override
	@Transactional
	public int updateDictInfo(TDictInfo tDictInfo) throws Exception {
		Date date = new Date();
		tDictInfo.setUpdateTime(date);
        int result = tDictInfoMapper.updateByPrimaryKey(tDictInfo);
        if(result >0){
    		TLog log = new TLog();
    		log.setDataId(tDictInfo.getId());
    		log.setTableName(tableDictInfoName);
    		log.setOperationType(2);
    		log.setCreateTime(date);
    		tLogMapper.insert(log);
        }
        return result;
	}

	@Override
	public TDictInfo getDictInfoById(long id) throws Exception {
		return tDictInfoMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<DictInfoVO> listDictInfoByTypeId(Long typeId) throws Exception {
		if(typeId == null || typeId <= 0){
            throw new RuntimeException("id不能为空或者少于等于0！");
        }
		return dictExtendMapper.listDictInfoByTypeId(typeId);
	}

	@Override
	public TDictType isHasDictTypeByIdNO(long id, String code) throws Exception {
		return dictExtendMapper.isHasDictTypeByIdNO(id, code);
	}
}
